import {DeleteOutlined, ExclamationCircleOutlined} from '@ant-design/icons';
import type {ActionType, ProColumns} from '@ant-design/pro-components';
import {FooterToolbar, PageContainer, ProTable,} from '@ant-design/pro-components';
import {Button, message, Modal} from 'antd';
import React, {useRef, useState} from 'react';
import type {LoginLogListItem} from "@/pages/log/LoginLogList/data";
import {queryLoginLogList, removeLoginLog} from "@/pages/log/LoginLogList/service";


/**
 *  Delete node
 * @zh-CN 删除节点
 *
 * @param selectedRows
 */
const handleRemove = async (selectedRows: LoginLogListItem[]) => {
  const hide = message.loading('正在删除');
  if (!selectedRows) return true;
  try {
    await removeLoginLog(
      selectedRows.map((row) => row.id),
    );
    hide();
    message.success('删除成功，即将刷新');
    return true;
  } catch (error) {
    hide();
    message.error('删除失败，请重试');
    return false;
  }
};

const LoginLogList: React.FC = () => {

  const actionRef = useRef<ActionType>();
  const [selectedRowsState, setSelectedRows] = useState<LoginLogListItem[]>([]);

  const showDeleteConfirm = (item: LoginLogListItem) => {
    Modal.confirm({
      title: '是否删除记录?',
      icon: <ExclamationCircleOutlined/>,
      content: '删除的记录不能恢复,请确认!',
      onOk() {
        handleRemove([item]).then(() => {
          actionRef.current?.reloadAndRest?.();
        });
      },
      onCancel() {
      },
    });
  };

  const columns: ProColumns<LoginLogListItem>[] = [
    {
      title: "id",
      dataIndex: 'id',
      hideInSearch: true,
    },
    {
      title: "用户id",
      dataIndex: 'userId',
    },
    {
      title: "用户名称",
      dataIndex: 'userName',
    },
    {
      title: "ip地址",
      dataIndex: 'ipAddress',
    },
    {
      title: "登录时间",
      dataIndex: 'createTime',
      hideInSearch: true,
    },
    {
      title: "操作",
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => [
        <>
          <Button
            type="primary"
            danger
            // disabled={!hasPm("/api/system/user/delete")}
            onClick={() => {
              showDeleteConfirm(record);
            }}
          >
            <DeleteOutlined/>删除
          </Button>
        </>
      ],
    },
  ];

  return (
    <PageContainer>
      <ProTable<LoginLogListItem, API.PageParams>
        actionRef={actionRef}
        rowKey="id"
        search={{
          labelWidth: 120,
        }}
        request={queryLoginLogList}
        columns={columns}
        rowSelection={{
          onChange: (_, selectedRows) => {
            setSelectedRows(selectedRows);
          },
        }}
        pagination={{pageSize: 10}}
      />

      {selectedRowsState?.length > 0 && (
        <FooterToolbar
          extra={
            <div>
              已选择 <a style={{fontWeight: 600}}>{selectedRowsState.length}</a> 项&nbsp;&nbsp;
            </div>
          }
        >
          <Button
            type={"primary"}
            danger
            onClick={async () => {
              await handleRemove(selectedRowsState);
              setSelectedRows([]);
              actionRef.current?.reloadAndRest?.();
            }}
          >
            <DeleteOutlined/>批量删除
          </Button>
        </FooterToolbar>
      )}
    </PageContainer>
  );
};

export default LoginLogList;
